package com.oyjd.business.sys.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.bean.vo.LoginLogVO;
import com.oyjd.bean.vo.MenuVO;
import com.oyjd.bean.vo.UserVO;
import com.oyjd.business.sys.service.SysService;
import com.oyjd.business.sys.util.SysUtil;
import com.oyjd.fw.Const;
import com.oyjd.fw.action.BaseAction;
import com.oyjd.fw.action.Page;
import com.oyjd.fw.util.OutUtil;
import com.oyjd.fw.util.SecurityUtil;

/**
 * 
 * 后台系统模块
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/sys")
@Results({ @Result(name = "goLogin", location = Const.JSP_goLogin), @Result(name = "goFramePage", location = Const.JSP_goFramePage), @Result(name = "goHead", location = Const.JSP_goHead), @Result(name = "goMenu", location = Const.JSP_goMenu), @Result(name = "goContent", location = Const.JSP_goContent), @Result(name = "viewLoginLog", location = Const.JSP_goLoginLog) })
public class SysAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4313775457664758514L;
	private static final Log log = LogFactory.getLog(SysAction.class);
	private SysService service;

	@Autowired
	public void setService(SysService service) {
		this.service = service;
	}

	/**
	 * 进入登录页面
	 * 
	 * @return
	 */
	public String goLogin() {
		return "goLogin";
	}

	/**
	 * 进入框架页面
	 * 
	 * @return
	 */
	public String goFramePage() {
		return "goFramePage";
	}

	/**
	 * 进入框架头部页面
	 * 
	 * @return
	 */
	public String goHead() {
		return "goHead";
	}

	/**
	 * 进入框架菜单页面
	 * 
	 * @return
	 */
	public String goMenu() {
		HttpServletRequest req = getRequest();
		UserVO userVO = (UserVO) session.get(Const.SessionKey);
		String roleName = SysUtil.role.get(userVO.getRoleId());
		req.setAttribute("role", roleName);
		return "goMenu";
	}

	/**
	 * 进入框架内容页面
	 * 
	 * @return
	 */
	public String goContent() {
		return "goContent";
	}

	/**
	 * 进入登入日志页面
	 * 
	 * @return
	 */
	public String viewLoginLog() {
		return "viewLoginLog";
	}

	/**
	 * 查询登入日志数据
	 * 
	 * @return
	 */
	public String getLoginLog() {
		Map<String, Object> map = Const.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<LoginLogVO> page = service.getLoginLog(new Page<LoginLogVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", Const.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", Const.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 用户登入
	 * 
	 * @return
	 */
	public String login() {
		Map<String, Object> map = Const.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String account = ServletRequestUtils.getStringParameter(req, "account");
			String pwd = ServletRequestUtils.getStringParameter(req, "pwd");

			pwd = SecurityUtil.encoderByMd5(pwd);

			int status = 1;
			UserVO userVO = service.getUser(account);
			if (null != userVO && pwd.equals(userVO.getPwd())) {
				session.put(Const.SessionKey, userVO);
				status = 0;
				map.put("info", userVO);
				map.put("code", Const.SUCCESS);
			}
			service.saveLoginLog(account, req.getRemoteAddr(), status);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", Const.UNKOWN_ERR);
		}

		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 根据角色获取平展菜单数据
	 * 
	 * @return
	 */
	public String getMenu() {
		Map<String, Object> map = Const.getJsonMap();
		try {
			UserVO userVO = (UserVO) session.get(Const.SessionKey);
			List<MenuVO> data = SysUtil.roleToFlatMenu.get(userVO.getRoleId());
			map.put("info", data);
			map.put("code", Const.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", Const.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取分类信息
	 * 
	 * @return
	 */
	public String toLink() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			MenuVO menuVO = SysUtil.menus.get(id);
			req.getRequestDispatcher(menuVO.getLink()).forward(req, getResponse());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}

	/**
	 * 登出
	 * 
	 * @return
	 * @throws IOException
	 */
	public String logout() throws IOException {
		HttpServletRequest req = getRequest();
		session.clear();
		req.getSession().invalidate();
		getResponse().sendRedirect("index.jsp");
		return null;
	}

}
